{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0.067732, 0.42781 , 0.995731, 0.738336, 0.981083]),\n",
       " array([3.176513, 3.816464, 4.550095, 4.256571, 4.560815]))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "#加载数据\n",
    "def load_data():\n",
    "    with open('简单线性数据.txt') as fr:\n",
    "        lines = fr.readlines()\n",
    "\n",
    "    x = np.empty(len(lines), dtype=float)\n",
    "    y = np.empty(len(lines), dtype=float)\n",
    "\n",
    "    for i in range(len(lines)):\n",
    "        line = lines[i].strip().split('\\t')\n",
    "        x[i] = line[0]\n",
    "        y[i] = line[1]\n",
    "\n",
    "    return x, y\n",
    "\n",
    "\n",
    "x, y = load_data()\n",
    "x[:5], y[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#常量\n",
    "N = len(x)\n",
    "w = 1.0\n",
    "b = 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAYTUlEQVR4nO3de5BcZZnH8d8znQ7pcMkEElYYMgYjiEBMgiMJFzEEEjGKRldhEbTK2jK1bu2WqEsJFiuwsgVr3IXdWheLUstVKYwiNQteNgk3WcBEEiYXAoQKlyQMsYgm4ZaBzOXdP8709CXd02/3nNP9ds/3U5UiM31O93sq4Zd3nvOe5zXnnAAA4Wpr9AAAAKMjqAEgcAQ1AASOoAaAw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+XKexJcnnursk/Ww4pKdJWmpmA8657voMMRG+f8//5Jx7U9KbZvawpDmSmjmofa77C5JudlHhdruZvSDpFEl/qM8QGya2XKtH6cNnc9x7JH1++C7pAkmvOud212FsSap43WbWKeluSZ9r8llVvorX7Zw70Tk30zk3U9Jdkv62yUNa8vt7/j+SPmhmE8xssqT5kp6u8zjj5nPdOxX9FCEz+wtJ75H0fF1H2Rix5VriM2pXZnNcM/ub4de/p+jO/1JJ2yUdUPQvcFPzvO5vSjpG0n8Nzy4HXJN3G/O87pbjc93OuafN7H8lbZY0JOn7zrmSS7uaheef97ck/cjMtigqB3zdOdf07U/N7E5JCyVNM7OXJF0nKS3Fn2s8Qg4AgePJRAAIHEENAIEjqAEgcAQ1AASOoAaAwBHUABA4ghoAAvf/3eUQ/1SM73MAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "def draw():\n",
    "    Y = np.empty(len(x))\n",
    "    for i in range(len(x)):\n",
    "        Y[i] = w * x[i] + b\n",
    "\n",
    "    plt.scatter(x, y)\n",
    "    plt.plot(x, Y, c='red')\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.067732"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#预测函数\n",
    "def predict(x):\n",
    "    return w * x + b\n",
    "\n",
    "\n",
    "predict(x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2250.4716967319127"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求loss,就是简单的做差求平方和\n",
    "def get_loss():\n",
    "    loss = 0\n",
    "    for i in range(N):\n",
    "        loss += np.power(predict(x[i]) - y[i], 2)\n",
    "    return loss\n",
    "\n",
    "\n",
    "get_loss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.6953226421712249, 3.3472825000000004)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#为了简化,这里假设只有2个数据\n",
    "#d_loss/d_w = 2*{w*(x1^2 + x2^2) - [(y1 - b)*x1 + (y2 - b)*x2]}\n",
    "#d_loss/d_b = 2*[2*b - (y1 - w*x1 + y2 - w*x2)]\n",
    "#因为是凸函数,整个函数中只有一点导数为0,也就是最低点,把上面两个求导公式等于0解方程\n",
    "#先解方程d_loss/d_b = 0\n",
    "#2*[2*b - (y1 - w*x1 + y2 - w*x2)] = 0\n",
    "#b = (y1 - w*x1 + y2 - w*x2)/2\n",
    "#整理成一般形式,这就是书上的公式了.\n",
    "#b = sigma(yi - w*xi)/N\n",
    "\n",
    "#再继续整理\n",
    "#b = (y1 + y2)/2 - w*(x1 + x2)/2\n",
    "#整理成一般形式\n",
    "#b = mean(y) - w*mean(x)\n",
    "\n",
    "#再解方程d_loss/d_w=0\n",
    "#2*{w*(x1^2 + x2^2) - [(y1 - b)*x1 + (y2 - b)*x2]} = 0\n",
    "#w*(x1^2 + x2^2) = (y1 - b)*x1 + (y2 - b)*x2\n",
    "#w*(x1^2 + x2^2) = (y1*x1 - b*x1) + (y2*x2 - b*x2)\n",
    "#w*(x1^2 + x2^2) = y1*x1 + y2*x2 - (b*x1 + b*x2)\n",
    "\n",
    "#把b带入\n",
    "#w*(x1^2 + x2^2) = y1*x1 + y2*x2 - {[mean(y) - w*mean(x)]*x1 + [mean(y) - w*mean(x)]*x2}\n",
    "#w*(x1^2 + x2^2) = y1*x1 + y2*x2 - {mean(y)*x1 + mean(y)*x2 - [w*mean(x)*x1 + w*mean(x)*x2]}\n",
    "#w*(x1^2 + x2^2) = y1*x1 + y2*x2 - mean(y)*[x1 + x2] + w*mean(x)*[x1 + x2]\n",
    "#w*(x1^2 + x2^2 - mean(x)*[x1 + x2]) = y1*x1 + y2*x2 - mean(y)*[x1 + x2]\n",
    "#w = {y1*x1 + y2*x2 - mean(y)*[x1 + x2]} / {x1^2 + x2^2 - mean(x)*[x1 + x2]}\n",
    "#整理成一般形式\n",
    "#w = {sigma(yi*xi) - mean(y)*sigma(xi)} / {sigma(xi^2) - mean(x)*sigma(xi)}\n",
    "\n",
    "#带入下面两个部分\n",
    "#mean(y)*[x1 + x2]\n",
    "#=[y1 + y2] * 1/2 * [x1 + x2]\n",
    "#=[y1 + y2] * [x1 + x2] * 1/2\n",
    "#=mean(x) * [y1 + y2]\n",
    "\n",
    "#mean(x)*[x1 + x2]\n",
    "#[x1 + x2] * 1/2 * [x1 + x2]\n",
    "#[x1 + x2]^2 * 1/2\n",
    "\n",
    "#w = {y1*x1 + y2*x2 - mean(x)*[y1 + y2]} / {x1^2 + x2^2 - [x1 + x2]^2 / 2}\n",
    "#w = {y1*x1 + y2*x2 - mean(x)*y1 - mean(x)*y2} / {x1^2 + x2^2 - [x1 + x2]^2 / 2}\n",
    "#w = {y1*[x1 - mean(x)] + y2*[x2 - mean(x)]} / {x1^2 + x2^2 - [x1 + x2]^2 / 2}\n",
    "#整理成一般形式,这就是书上的公式了.\n",
    "#w = sigma(yi*[xi - mean(x)]) / {sigma(xi^2) - sigma(xi)^2 / 2}\n",
    "\n",
    "\n",
    "def get_w_b():\n",
    "    #b = mean(y) - w*mean(x)\n",
    "    _b = np.mean(y) - w * np.mean(x)\n",
    "\n",
    "    #w = sigma(yi*[xi - mean(x)]) / {sigma(xi^2) - sigma(xi)^2 / 2}\n",
    "    fenzi = 0\n",
    "    for i in range(N):\n",
    "        fenzi += y[i] * (x[i] - np.mean(x))\n",
    "\n",
    "    _w = fenzi / (np.power(x, 2).sum() - np.power(x.sum(), 2) / N)\n",
    "\n",
    "    return _w, _b\n",
    "\n",
    "\n",
    "get_w_b()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.41263053157602\n",
      "1.355249081681491\n",
      "1.355249081681491\n",
      "1.355249081681491\n",
      "1.355249081681491\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD5CAYAAAAp8/5SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3de3xU1bXA8d/KMOCAlkANVoIBrFbUi4AG8BZrhar4QIyoFcTaWivFR+ujpUIfQq0VlNtqtSoiWq1PRDFSFdEWuSotSDAgImCtYGDoFRQCAinkse4fZ95zZuZMmCRksr6fDx9zzuw5s0+ha3bWXmdvUVWMMcbkr4KW7oAxxpimZYHeGGPynAV6Y4zJcxbojTEmz1mgN8aYPGeB3hhj8lw7rw1FxAdUAEFVHZHw2gRgbMw1jwWKVHWbiGwAvgDqgTpVLc30WYceeqj26tXLa9eMMabNW758+WeqWuT2mudAD1wPrAG+lPiCqk4HpgOIyHnAjaq6LabJUFX9zOsH9erVi4qKiiy6ZowxbZuIfJLqNU+pGxHpAZwLzPLQfAzwtLeuGWOMaWpec/R3Az8DGtI1EpGOwFnA8zGnFXhNRJaLyLhG9dIYY0yjZQz0IjIC2KKqyz1c7zxgcULaZoiqngicDVwrIqem+JxxIlIhIhVbt2710ndjjDEeeBnRDwFGhiZVnwGGicgTKdqOJiFto6qbQ//dArwADHJ7o6rOVNVSVS0tKnKdTzDGGNMIGQO9qk5S1R6q2gsnkC9U1csS24lIZ+CbwIsx5zqJyCHhn4Ezgf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vvOPlvlV1jYi8CrwHNACzVNW1NK+18Pj3/RvgURFZhZPOuFlVW/2qliLyNHAacKiIbAImA37IfVyzJ2ONMSbPtYbUjTHGmP1ggd4YY/KcBXpjjMlzFuiNMSbPWaA3xpg8Z4HeGGPynAV6Y4zJcxbojTEmz/0/2Jbjb6bOm1kAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(1.6953226421712249, 3.0077432426975896)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#慢慢调整w和b\n",
    "for i in range(5):\n",
    "    w, b = get_w_b()\n",
    "    print(get_loss())\n",
    "draw()\n",
    "w, b"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
